<?php
	require_once("jara_config.php");
	require_once("db_fns.php");
	
	function jara_user_authenticate($username, $password) {
		$query = "select * from jara_users where username = '$username' and password = SHA1('$password') limit 1";
		$result = jara_db_query($query);
		if($result->num_rows == 0) {
			return false;
		}
		else {
			$row = $result->fetch_assoc();
			$_SESSION["jara_admin"] = $username;
			$_SESSION["jara_permissions"] = array($row["permission_posts"], $row["permission_pages"], $row["permission_users"], $row["permission_upload"]);
			return true;
		}
	}
	
	function jara_user_create($username, $password, $permissions) {
		$check_query = "select * from jara_users where username = '$username'";
		$check_result = jara_db_query($check_query);
		if($check_result->num_rows != 0) {
			throw new JaraGeneralException("A user already exists with the username $username.");
		}
		$query = "insert into jara_users values('0', '$username', SHA1('$password'), '{$permissions[0]}', '{$permissions[1]}', '{$permissions[2]}', '{$permissions[3]}')";
		$result = jara_db_query($query);
		if($result == 0) {
			return false;
		}
		else {
			return true;
		}
	}
?>